<!--地址列表-->
<template>
  <div class="adr-wrap">
    <div class="adr-row"  v-for="item in todolist">
      <p class="txt">录入时间：{{item.CreateTime | timeshort}}</p>
      <p class="txt">在途信息：{{item.Memo}}</p>
      <div class="adr-ope displayflex">
        <div class="flex1"></div>
        <span class="adr-btn" @click="editMsg(item)"><i class="llse llse-bianji"></i>编辑</span>
        <span class="adr-btn" @click="delMsg(item.Guid)"><i class="llse llse-shanchu"></i>删除</span>
      </div>
    </div>
    <div class="btn-fixed-large" @click="showDialog"><i class="iconfont icon-add"></i><span>新增信息</span></div>
    <div class="noDate_page" v-if="todolist.length==0">
      <div class="noDate_icon">
        <i class="iconfont icon-null"></i>
      </div>
      暂无在途信息，请新增
    </div>
    <!-- 弹框start -->
    <x-dialog
      :show.sync="popupVisible" class="bill-popup">
      <div class="load-dialog">
        <p>请输入在途信息</p>
        <textarea placeholder="请输入在途信息（1-200字符）" maxlength="200" v-model="msg"></textarea>
      </div>
      <div class="load-btn displayflex">
        <span class="flex1" @click="closeBillDate">取消</span>
        <span class="flex1" @click="addMsg">确定</span>
      </div>
    </x-dialog>
    <!-- 弹框end -->
  </div>
</template>

<script>
import { XDialog } from 'vux';
export default {
  data() {
    return {
      popupVisible:false,
      msg:'',
      todolist:'',
      index: -1,
      mainData: {},
    }
  },
  components:{
    XDialog
  },
  computed:{
    guid(){
      return this.$route.query.Guid
    },
    billno(){
      return this.$route.query.billno
    },
    userinfo(){
      return localStorage.userinfo?JSON.parse(localStorage.userinfo):null; 
    }
  },
  created(){
    this.getData();
  },
  methods: {
    showDialog(){
      this.popupVisible = true;
      this.msg = '';
    },

    closeBillDate(){
      this.popupVisible=false;
      this.index = -1;
      this.msg = '';
    },

    addMsg(){
      this.popupVisible=false;
    },

    getData(){
      let query = {
        Guid: this.$route.query.Guid,
        QueryType: 'Tracing'
      };
      this.Ajax.post('GetEnrouteList', query).then((res) => {
        if (res.IsSuccess == true) {
          if (res.Datas.length > 0) {
            this.todolist = res.Datas[0].TracingList;
            this.mainData = res.Datas[0];
          }
        } else {
          this.Toast({
            message: '发生错误，请刷新重试'
          });
        };
      }).catch(e => {

      });
    },
    
    addMsg(){
      var query,text;

      if (!this.msg) {
        this.Toast({
            message: '文本框不能为空'
        });
        return false;
      };

       //更新
      if(this.editTrue) {
        if(this.index > -1) {
          query = this.$deepcopy(this.todolist[this.index]);
          query.Memo = this.msg;
          query.CreateUserID = this.userinfo.StaffID;
          query.CreateUserName = this.userinfo.Name;
        }
        text = '修改成功';
      } else {
        query = {
          BBillNo: this.mainData.BBillNo,
          BBillGuid: this.mainData.BBillGuid,
          TenantGuid: this.userinfo.TenantGuid,
          CorpGuid: this.userinfo.CorpGuid,
          DeliverGuid: this.userinfo.BelongGuid,
          CargoOwnerGuid: this.mainData.CargoOwnerGuid,
          LogCenterGuid: this.mainData.LogCenterGuid,
          WareHouseGuid: this.mainData.WarehouseGuid,
          TPLPoGuid: this.guid,
          TPLPoNo: this.mainData.TPLPoNo,
          Memo: this.msg,
          CreateUserID: this.userinfo.StaffID,
          CreateUserName: this.userinfo.Name
        }
        text = '保存成功';
      }
      
      this.Ajax.post('ManualTracing', query).then((res) => {
        if (res.IsSuccess == true) {
          this.popupVisible = false;
          this.getData();
          this.Toast({
              message: text
          });
        } else {
          this.Toast({
              message: '发生错误，请刷新重试',
          });
        };
      }).catch(e => {
        console.log(e);
        this.Toast({
            message: '发生错误，请刷新重试',
        });
      });
    },

    delMsg(val){
      let query = {
        Guid:val,
        DeleteSign: 'Y',
      };
      this.Ajax.post('ManualTracing', query).then((res) => {
        if (res.IsSuccess == true) {
          this.getData();
          this.Toast({
              message: '删除成功',
          });
        } else {
          this.Toast({
              message: '发生错误，请刷新重试',
          });
        };
      }).catch(e => {
        console.log(e);
        this.Toast({
            message: '发生错误，请刷新重试',
        });
      });
    },

    editMsg(val){
      this.msg = val.Memo;
      this.popupVisible = true;
      this.editTrue = true;
      this.editGuid = val.Guid;
      this.index = this.todolist.findIndex(x => {
        return x.Guid == val.Guid;
      })
    },
  },
}
</script>
